package com.tourex.utils;

import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.Interpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;

public class AnimUtils {

	public static final int DEFAULT_DURATION = 800;

	private AnimationSet set;

	private AnimUtils() {
	}

	public static AnimUtils init() {
		AnimUtils animUtils = new AnimUtils();
		animUtils.set = new AnimationSet(false);
		return animUtils;
	}

	public AnimUtils addAlpha(float fromAlpha, float toAlpha) {
		AlphaAnimation alpha = new AlphaAnimation(fromAlpha, toAlpha);
		alpha.setDuration(DEFAULT_DURATION);
		set.addAnimation(alpha);
		return this;
	}

	public AnimUtils addAlpha(float fromAlpha, float toAlpha, long offset,
			long duration, Interpolator interpolator, int repeatMode) {
		AlphaAnimation alpha = new AlphaAnimation(fromAlpha, toAlpha);
		alpha.setDuration(duration);
		alpha.setStartOffset(offset);
		alpha.setInterpolator(interpolator);
		alpha.setRepeatMode(repeatMode);
		if (repeatMode == Animation.REVERSE)
			alpha.setRepeatCount(1);
		set.addAnimation(alpha);
		return this;
	}

	public AnimUtils addScale(float fromX, float toX, float fromY, float toY,
			float pivotX, float pivotY) {
		ScaleAnimation scale = new ScaleAnimation(fromX, toX, fromY, toY,
				Animation.RELATIVE_TO_SELF, pivotX, Animation.RELATIVE_TO_SELF,
				pivotY);
		scale.setDuration(DEFAULT_DURATION);
		set.addAnimation(scale);
		return this;
	}

	public AnimUtils addScale(float fromX, float toX, float fromY, float toY,
			int pivotXType, float pivotXValue, int pivotYType,
			float pivotYValue, long offset, long duration,
			Interpolator interpolator) {
		ScaleAnimation scale = new ScaleAnimation(fromX, toX, fromY, toY,
				pivotXType, pivotXValue, pivotYType, pivotYValue);
		scale.setDuration(duration);
		scale.setStartOffset(offset);
		scale.setInterpolator(interpolator);
		set.addAnimation(scale);
		return this;
	}

	public AnimUtils addTranslate(float fromXDelta, float toXdelta,
			float fromYdelta, float toYDelta) {
		TranslateAnimation translate = new TranslateAnimation(fromXDelta,
				toXdelta, fromYdelta, toYDelta);
		translate.setDuration(DEFAULT_DURATION);
		set.addAnimation(translate);
		return this;
	}

	public AnimUtils addTranslate(float fromXDelta, float toXdelta,
			float fromYdelta, float toYDelta, long offset, long duration,
			Interpolator interpolator) {
		TranslateAnimation translate = new TranslateAnimation(fromXDelta,
				toXdelta, fromYdelta, toYDelta);
		translate.setDuration(duration);
		translate.setStartOffset(offset);
		translate.setInterpolator(interpolator);
		set.addAnimation(translate);
		return this;
	}

	public AnimUtils addTranslate(int fromXType, float fromXValue, int toXType,
			float toXValue, int fromYType, float fromYValue, int toYType,
			float toYValue, long offset, long duration,
			Interpolator interpolator) {
		TranslateAnimation translate = new TranslateAnimation(fromXType,
				fromXValue, toXType, toXValue, fromYType, fromYValue, toYType,
				toYValue);
		translate.setDuration(duration);
		translate.setStartOffset(offset);
		translate.setInterpolator(interpolator);
		set.addAnimation(translate);
		return this;
	}

	public AnimUtils addRotate(float fromDegrees, float toDegrees,
			float pivotX, float pivotY) {
		RotateAnimation rotate = new RotateAnimation(fromDegrees, toDegrees,
				Animation.RELATIVE_TO_SELF, pivotX, Animation.RELATIVE_TO_SELF,
				pivotY);
		rotate.setDuration(DEFAULT_DURATION);
		set.addAnimation(rotate);
		return this;
	}

	public AnimUtils addRotate(float fromDegrees, float toDegrees,
			int pivotXType, float pivotXValue, int pivotYType,
			float pivotYValue, long offset, long duration,
			Interpolator interpolator) {
		RotateAnimation rotate = new RotateAnimation(fromDegrees, toDegrees,
				pivotXType, pivotXValue, pivotYType, pivotYValue);

		rotate.setDuration(duration);
		rotate.setStartOffset(offset);
		rotate.setInterpolator(interpolator);
		set.addAnimation(rotate);
		return this;
	}

	public AnimationSet create() {
		return set;
	}
	
	public static AnimationSet cearteAlpha(float fromAlpha,float toAlpha){
		return AnimUtils.init().addAlpha(fromAlpha, toAlpha).create();
	}
}
